
import { ElMessage } from "element-plus";
/**
 * 插件相关服务
 */
 class PluginService {
    constructor(store, config) {
        this.store = store;
        this.config = config;
        this.plugins = {};
    }


    /**
     * 添加插件
     * @param {id} pluginId 
     * @param {对象} pluginJs 
     */
    addPlugin(pluginId, pluginJs) {
        this.plugins[pluginId] = pluginJs;
    }


    /**
     * 移除插件
     * @param {插件id} pluginId 
     */
    removePlugin(pluginId) {
        this.plugins[pluginId] = undefined;
    }

    /**
     * 获取插件
     * @param {插件id} pluginId 
     * @returns 
     */
    getPlugin(pluginId){
        const pluginJs =  this.plugins[pluginId];
        if(!pluginJs){
            ElMessage.error("插件未安装，无法加载数据");
            return null;
        }
        return pluginJs;
    }


    /**
     * 获取该节点下的孩子节点信息
     * @param {资源节点} node 
     * @returns 
     */
    getMetadata(node) {
        return new Promise((resolve, reject) => {
            const pluginJs = this.getPlugin(node.data.properties.pluginId);
            if (!pluginJs) {
                ElMessage.error("插件未安装，无法加载数据");
                reject();
                return;
            }
            const resource = pluginJs.resource[
                node.data.type.toLowerCase()
            ];

            if (resource) {
                resource.metadata({ node: node })
                    .then(res => {
                        resolve(res)
                    })
                    .catch(e => reject(e));
            }
        })
    }

}

export {
    PluginService
}